package com.gxa.autoparts.portal.service.impl;

import com.gxa.autoparts.core.pojo.Goods;
import com.gxa.autoparts.core.pojo.GoodsSku;
import com.gxa.autoparts.core.util.JsonUtil;
import com.gxa.autoparts.core.util.SnowFlakeUtil;
import com.gxa.autoparts.portal.config.RabbitConfig;
import com.gxa.autoparts.portal.service.OrderService;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author R
 * @data 2022-01-10 14:32
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Override
    public Long addOrder(Long userId, Long addressId, List<Map<String,Object>> skuWithNumber, String remarks, Long couponId, Double freight) {
        HashOperations<String, Object, Object> hashOps = redisTemplate.opsForHash();
        Long orderId = SnowFlakeUtil.nextId();
        if (couponId == null){
            couponId = 0L;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("orderId",orderId);
        map.put("userId",userId);
        map.put("addressId",addressId);
        map.put("skuWithNumber",skuWithNumber);
        map.put("remarks",remarks);
        map.put("couponId",couponId);
        map.put("freight",freight);
        String s = JsonUtil.ObjectToString(map);
        for (Map<String, Object> stringLongMap : skuWithNumber) {
            System.out.println(stringLongMap.get("skuId")+"-------------");
            Long skuId = Long.parseLong(stringLongMap.get("skuId").toString());
//            redisTemplate.delete("SKU:"+skuId);
            Map<Object, Object> entries = hashOps.entries("SKU:" + skuId);
            GoodsSku goodsSku = JsonUtil.mapToObject(entries, GoodsSku.class);
            Long goodsId = goodsSku.getGoodsId();
//            redisTemplate.delete("GOOD:"+goodsId);
        }
        // 添加到正常的添加到数据库的队列
        rabbitTemplate.convertAndSend(RabbitConfig.SELL_EXCHANGE,RabbitConfig.SELL_KEY,s);
        // 添加到有过期时间的队列
        rabbitTemplate.convertAndSend(RabbitConfig.ORDER_EXCHANGE,RabbitConfig.ORDER_KEY,orderId,messagePostProcessor());
        return orderId;
    }

    //发送消息的处理方法, 添加过期时间
    private MessagePostProcessor messagePostProcessor(){
        return new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //设置有效期30分钟(单位毫秒)
                //message.getMessageProperties().setExpiration("1800000");
                //测试10秒后过期
                message.getMessageProperties().setExpiration("10000");
                return message;
            }
        };
    }

}
